<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use Core\AdminController;
use Models\Question;
class QuestionController extends AdminController {

	protected $question_attr = array();
	protected $question_types = array();

	public function __construct(){
		parent::__construct();
		
		$config = CONFIG('basic');
		$this->question_attr = array(
					'cognitive_ability'=>$config['cognitive_ability'],
					'client_needs'=>$config['client_needs'],
					'nursing_process'=>$config['nursing_process'],
					'content_area'=>$config['content_area']
				);
		$this->question_types = $config['question_types'];
	}

	/**
	 * 题目列表页面
	 */
	public function index(){
		$data['question_attr'] = $this->question_attr;
		$data['question_types'] = $this->question_types;
		$data['params'] = array(
			'type'=>$this->input->get('type'),
			'score'=>$this->input->get('score'),
			'keyword'=>$this->input->get('keyword'),
			'cognitive_ability'=>$this->input->get('cognitive_ability'),
			'client_needs'=>$this->input->get('client_needs'),
			'nursing_process'=>$this->input->get('nursing_process'),
			'content_area'=>$this->input->get('content_area')
		);

		$question = new Question;
		foreach ($data['params'] as $key => $value) {
			if($value && $key != 'keyword'){
				$question->where($key,$value);
			}
		}

		if($data['params']['keyword']){
			$question->group_start()
						 ->like('content',$data['params']['keyword'])
						 ->or_like('rationale',$data['params']['keyword'])
						 ->or_like('strategy',$data['params']['keyword'])
					 ->group_end();
		}

		$data['questions'] = $question->paginate(20);

		$this->load->view('admin/question/list',$data);
	}

	

	/**
	 * 添加题目页面
	 */
	public function add(){
		$data['question_attr'] = $this->question_attr;
		$data['question_types'] = $this->question_types;
		$data['question'] = array();
		$data['back_url'] = $this->input->server('HTTP_REFERER');
		$this->load->view('admin/question/add',$data);
	}

	/**
	 * 添加题目页面
	 */
	public function edit($id){
		$data['question_attr'] = $this->question_attr;
		$data['question_types'] = $this->question_types;
		$question = new Question;
		$data['question'] = $question->find(intval($id));
		$data['back_url'] = $this->input->server('HTTP_REFERER');
		$this->load->view('admin/question/add',$data);
	}

	/**
	 * 删除
	 */
	public function delete(){
		$question = new Question;
		$question->delete($this->input->post('id'));
		echo json_encode(array('code'=>0,'msg'=>'ok'));
	}


	/**
	 * 提交保存
	 */
	public function save(){
		$data['course_id'] = 1;
		$data['type'] = intval($this->input->post('type'));
		$data['score'] = intval($this->input->post('score'));
		$data['content'] = $this->input->post('content');
		$data['content_cn'] = $this->input->post('content_cn');
		$data['rationale'] = $this->input->post('rationale');
		$data['rationale_cn'] = $this->input->post('rationale_cn');
		$data['strategy'] = $this->input->post('strategy');
		$data['strategy_cn'] = $this->input->post('strategy_cn');
		$data['cognitive_ability'] = $this->input->post('cognitive_ability');
		$data['client_needs'] = $this->input->post('client_needs');
		$data['nursing_process'] = $this->input->post('nursing_process');
		$data['content_area'] = $this->input->post('content_area');
		
		$data['others'] = '';
		if($this->input->post('others_form_title')){
			$o_titles = $this->input->post('others_form_title');
			$o_content = $this->input->post('others_form_content');
			$others = array();
			$i = 0;
			foreach ($o_titles as $t) {
				$others[$t] = $o_content[$i];
				$i++;
			}
			$data['others'] = json_encode($others);
		}

		switch($data['type']){
			case 1: //单选
			case 2: //多选
			case 5: //排序
				$data['answer'] = $this->input->post('answer');
				$data['options'] = json_encode($this->input->post('options_from_content'));
				break;
			case 3: //填空题
				$data['answer'] = json_encode($this->input->post('answer'));
				$data['options'] = '';
				break;
			case 4: //选图题
				$data['options'] = $this->input->post('options_from_content');
				$data['answer'] = $this->input->post('answer');
				break;
		}

		$question = new Question;
		if(intval($this->input->post('id'))){
			$question->update(intval($this->input->post('id')),$data);
			echo json_encode(array('code'=>0,'msg'=>'ok','question_id'=>$this->input->post('id')));
		}else{
			$data['create_time'] = date('Y-m-d H:i:s');
			$question_id = $question->add($data);
			if($question_id){
				echo json_encode(array('code'=>0,'msg'=>'ok','question_id'=>$question['id']));	
			}else{
				echo json_encode(array('code'=>1,'msg'=>'添加失败'));
			}
		}

		

	}

}
